package com.zeei.ems.meet.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeei.ems.base.page.PageParamVO;
import com.zeei.ems.base.util.DateUtils;
import com.zeei.ems.base.util.JsonUtil;
import com.zeei.ems.base.util.MQPublishUtil;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.meet.constant.MQConstant;
import com.zeei.ems.meet.dao.TaskNewDao;
import com.zeei.ems.meet.model.TaskNewVO;
import com.zeei.ems.meet.model.vo.TaskNewParam;
import com.zeei.ems.meet.service.TaskNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class TaskNewServiceImpl implements TaskNewService {
    @Autowired
    TaskNewDao taskNewDao;

    @Override
    public PageInfo<TaskNewVO> listTaskNew(PageParamVO page, TaskNewParam taskNewParam) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<TaskNewVO> taskNewList = taskNewDao.listTaskNew(taskNewParam);
        return new PageInfo<>(taskNewList);
    }

    @Override
    public List<TaskNewVO> listTaskNew(TaskNewParam taskNewParam) {
        return taskNewDao.listTaskNew(taskNewParam);
    }

    @Override
    public List<TaskNewVO> listTaskNewCurrent() {
        TaskNewParam taskNewParam = new TaskNewParam();
        taskNewParam.setIsSubmit(0);
        List<TaskNewVO> taskNewList = taskNewDao.listTaskNew(taskNewParam);
        if (!CollectionUtils.isEmpty(taskNewList)){
            taskNewList.forEach(task -> {
                if (task.getEmergencyTypeTime() != null) {
                    Double dMinute = task.getEmergencyTypeTime() * 60;
                    Integer minute = dMinute.intValue();
                    DateTime dateTime = new DateTime(task.getCreateTime());
                    dateTime = dateTime.offset(DateField.MINUTE, minute);
                    if (dateTime.before(new DateTime())) {
                        task.setIsOverdue(1);
                    } else {
                        task.setIsOverdue(0);
                    }
                }else{
                    task.setIsOverdue(0);
                }
            });
        }

        return taskNewList;
    }

    @Override
    public Boolean addTaskNew(TaskNewVO taskNew) {
        String taskCode = getTaskCode(taskNew.getPointCode().toString());
        taskNew.setTaskCode(taskCode);
        taskNew.setCreateUser(1);
        taskNew.setIsSubmmit(0);
        if (taskNew.getTaskStatus() == null){
            taskNew.setTaskStatus(0);
        }

        Integer num = taskNewDao.addTaskNew(taskNew);
        if (num > 0){
            sendTaskMq(taskNew);
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }

    @Override
    public Boolean downTaskNew(Integer taskId){
        TaskNewParam taskNewParam = new TaskNewParam();
        taskNewParam.setTaskId(taskId);
        List<TaskNewVO> taskNewList = taskNewDao.listTaskNew(taskNewParam);
        if (!CollectionUtils.isEmpty(taskNewList)){
            TaskNewVO taskNew = taskNewList.get(0);
            taskNew.setTaskStatus(1);
            taskNew.setIsSubmmit(0);
            if (updateTaskNew(taskNew)) {
                //sendTaskMq(taskNew);

                return Boolean.TRUE;
            }
        }

        return Boolean.FALSE;
    }

    @Override
    public Boolean updateTaskNew(TaskNewVO taskNew) {
        Integer integer = taskNewDao.updateTaskNew(taskNew);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteTaskNew(List<Integer> taskIdList) {
        Integer integer = taskNewDao.deleteTaskNew(taskIdList);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 获取任务编码
     * @param pointCode 站点编码
     * @return 返回任务编码
     */
    private String getTaskCode(String pointCode){
        String taskCode = StringUtil.empty();
        String pointAreaCode = taskNewDao.getPointAreaCode(pointCode);
        if (StringUtil.isNotEmpty(pointAreaCode)){
            String strDate = DateUtils.dateToDateString(DateUtils.getTodayDate(), DateUtils.DATE_STR_FORMAT);
            taskCode = String.format("01%s_%s", pointAreaCode, strDate);
            String taskCodeNum = taskNewDao.getTaskCodeNum(taskCode);
            String len = "0" + taskCodeNum;
            len = len.substring(len.length() - 2);
            taskCode = String.format("%s_%s", taskCode, len);
        }

        return taskCode;
    }

    /**
     * 推送到任务队列
     * @param taskNew 任务实体
     */
    private void sendTaskMq(TaskNewVO taskNew){
        if (taskNew.getTaskStatus().equals(1)){
            String strTask = JsonUtil.object2json(taskNew);
            MQPublishUtil.sendQueueMsg(MQConstant.TASK_NEW, strTask);
        }
    }
}